﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.Task_;
using CommonLibrary;
using CommonLibrary.BLL;
using CommonLibrary.Model;

namespace SmsSeries
{
    public partial class frmMain : Form
    {
        #region 变量

        private frmSendMsg frmsendMsg;

        private Task taskLoadMsgList;

        #endregion

        #region 属性

        /// <summary>
        /// 用户ID
        /// </summary>
        private int UserID
        {
            get; set;
        }

        #endregion

        #region 控件事件

        // 构造函数
        public frmMain(int userID)
        {
            UserID = userID;
            frmsendMsg=new frmSendMsg(userID);
            InitializeComponent();
        }

        //窗体加载
        private void frmMain_Load(object sender, EventArgs e)
        {
            dtStartTime.Value = DateTime.Now.Date;

            dtEndTime.Value = DateTime.Now.Date;
            dtEndTime.Value = DateTime.Now.Date.AddHours(23).AddMinutes(59).AddSeconds(59);

            Data.ComboBoxItem comboBoxItem0=new Data.ComboBoxItem(0,"短信内容");
            Data.ComboBoxItem comboBoxItem1=new Data.ComboBoxItem(1,"目标号码");
            //Data.ComboBoxItem comboBoxItem2=new Data.ComboBoxItem(2,"端口号");
            //Data.ComboBoxItem comboBoxItem3 = new Data.ComboBoxItem(3, "短信状态");
            Data.ComboBoxItem comboBoxItem4 = new Data.ComboBoxItem(4, "短信签名");
            //Data.ComboBoxItem comboBoxItem5=new Data.ComboBoxItem(5,"短信内容");

            cbSearch.Items.Add(comboBoxItem0);
            cbSearch.Items.Add(comboBoxItem1);
            //cbSearch.Items.Add(comboBoxItem2);
            //cbSearch.Items.Add(comboBoxItem3);
            cbSearch.Items.Add(comboBoxItem4);

            if (cbSearch.Items.Count > 0)
            {
                cbSearch.SelectedIndex = 0;
            }

            LoadListView();

            CycExecution cycShedule =
            new CycExecution(DateTime.Now, new TimeSpan(0, 0, 0, 30));
            taskLoadMsgList = new Task(cycShedule);
            taskLoadMsgList.ExecuteEVEvent += new EventHandler<Data.TwoEventArgs>(taskLoadMsgList_ExecuteEVEvent);
            taskLoadMsgList.StartTask();
        }

        //任务 加载列表
        void taskLoadMsgList_ExecuteEVEvent(object sender, Data.TwoEventArgs e)
        {
            this.BeginInvoke((MethodInvoker) delegate
                                                 {
                                                     LoadListView();
                                                 });
        }

        //窗体 关闭前
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show(this, "确定退出短信群发系统吗？", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                System.Environment.Exit(0);
            }
            else
            {
                e.Cancel = true;
            }
        }

        //工具栏 退出
        private void tsbQuit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "确定退出短信群发系统吗？", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                System.Environment.Exit(0);
            }
        }

        //工具栏 发送短信
        private void tsbSendMsg_Click(object sender, EventArgs e)
        {
            frmsendMsg.ShowDialog(this);
        }

        //工具栏 帮助
        private void tsbHelp_Click(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.ShowDialog();
        }

        //工具栏 开始时间改变
        private void dtStartTime_ValueChanged(object sender, EventArgs e)
        {
            LoadListView();
        }

        //工具栏 结束时间改变
        private void dtEndTime_ValueChanged(object sender, EventArgs e)
        {
            LoadListView();
        }

        //工具栏 查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadListView();

            cbSearch.Text = "";
        }

        #endregion

        #region 方法

        /// <summary>
        /// 加载发送记录信息
        /// </summary>
        /// <param name="sentRecordInfos"></param>
        /// <param name="imageIndex">发送记录</param>
        void LoadSentTaskListListView(List<SendTaskInfo> sentTaskInfos, int imageIndex)
        {
            lvData.Clear();

            //添加标头
            ColumnHeader title = new ColumnHeader();
            title.Text = "编号";
            title.Width = 50;
            this.lvData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "发送时间";
            title.Width = 130;
            this.lvData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "目标号码";
            title.Width = 200;
            this.lvData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "短信内容";
            title.Width = 650;
            this.lvData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "短信签名";
            title.Width = 100;
            this.lvData.Columns.Add(title);

            title = new ColumnHeader();
            title.Text = "发送状态";
            title.Width = 130;
            this.lvData.Columns.Add(title);

            //title = new ColumnHeader();
            //title.Text = "端口号";
            //title.Width = 80;
            //this.lvData.Columns.Add(title);

            string key;
            lvData.BeginUpdate();

            sentTaskInfos = sentTaskInfos.OrderBy(o => o.TaskID).ToList();

            foreach (SendTaskInfo sendTaskInfo in sentTaskInfos)
            {
                key = sendTaskInfo.TaskID.ToString();
                lvData.Items.Add(key, key, imageIndex);
                lvData.Items[key].SubItems.Add(sendTaskInfo.SendTime.ToString());
    
                lvData.Items[key].SubItems.Add(sendTaskInfo.DestNumber);
                lvData.Items[key].SubItems.Add(sendTaskInfo.Content);
                lvData.Items[key].SubItems.Add(sendTaskInfo.SignName.ToString());

                string s = "";
                switch ((Data.StateEnum)sendTaskInfo.SendFlag)
                {
                    case Data.StateEnum.MsgExpired:
                        s = "消息过期";
                        break;
                    case Data.StateEnum.SendedFalse:
                        s = "发送失败";
                        break;
                    case Data.StateEnum.SendedSuccess:
                        s = "发送成功";
                        break;
                    case Data.StateEnum.Sending:
                        s = "正在发送";
                        break;
                    case Data.StateEnum.UserExpired:
                        s = "用户过期";
                        break;
                    case Data.StateEnum.UserIsLocked:
                        s = "用户被锁定";
                        break;
                    case Data.StateEnum.UserNotExist:
                        s = "用户不存在";
                        break;
                    case Data.StateEnum.WaitSend:
                        s = "待发送";
                        break;
                }

                lvData.Items[key].SubItems.Add(s);
                //lvData.Items[key].SubItems.Add(sendTaskInfo.CommPort.ToString());

                lvData.Items[key].SubItems.Add(key);
            }

            lvData.EndUpdate();
        }

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="isSendSuccess"></param>
        private void LoadListView()
        {
            //if (!isSendSuccess)
            //{
            //    List<SendTaskInfo> _sentTaskInfos = SendTask.GetList("");
            //    _sentTaskInfos =
            //        _sentTaskInfos.Where(o => o.SendFlag != (int)Data.StateEnum.SendedSuccess
            //        && o.SendTime >= dtStartTime.Value
            //        && o.SendTime <= dtEndTime.Value).ToList();

            //    LoadSentTaskListListView(_sentTaskInfos, 2);
            //}
            //else
            //{
            List<SendTaskInfo> _sentTaskInfos = SendTask.GetList("");

            if (tbSearch.Text == "")
            {
                _sentTaskInfos = _sentTaskInfos.Where(o =>
                    //o.SendFlag == (int)Data.StateEnum.SendedSuccess
                    //&& 
                    o.SendTime >= dtStartTime.Value
                    && o.SendTime <= dtEndTime.Value).ToList();

                LoadSentTaskListListView(_sentTaskInfos, 2);

            }
            else
            {
                switch (((Data.ComboBoxItem)cbSearch.SelectedItem).ID)
                {
                    case 0:
                        //短信内容
                        _sentTaskInfos = _sentTaskInfos.Where(o =>
                            o.Content.Contains(tbSearch.Text)
                           && o.SendTime >= dtStartTime.Value
                           && o.SendTime <= dtEndTime.Value).ToList();

                        LoadSentTaskListListView(_sentTaskInfos, 2);
                        break;
                    case 1:
                        //目标号码
                        _sentTaskInfos = _sentTaskInfos.Where(o =>
                            o.DestNumber.Contains(tbSearch.Text)
                           && o.SendTime >= dtStartTime.Value
                           && o.SendTime <= dtEndTime.Value).ToList();

                        LoadSentTaskListListView(_sentTaskInfos, 2);
                        break;
                    //case 2:
                    //    //端口号
                    //    _sentTaskInfos = _sentTaskInfos.Where(o =>
                    //        o.CommPort.ToString() == tbSearch.Text
                    //       && o.SendTime >= dtStartTime.Value
                    //       && o.SendTime <= dtEndTime.Value).ToList();

                    //    LoadSentTaskListListView(_sentTaskInfos, 2);
                    //    break;
                    //case 3:
                    //    //短信状态
                    //    break;
                    case 4:
                        //短信签名
                        _sentTaskInfos = _sentTaskInfos.Where(o =>
                            o.SignName.Contains(tbSearch.Text)
                           && o.SendTime >= dtStartTime.Value
                           && o.SendTime <= dtEndTime.Value).ToList();

                        LoadSentTaskListListView(_sentTaskInfos, 2);
                        break;
                        
                }
            }
            //}
        }

        #endregion
    }
}
